<?php
class MailSender {
    private $mail = null;
    private $mailer = 'smtp';
    private $mailSize= 50;
    private $mailTo = array();
    private $sendCount = 0;
    private $sendFail = array();
    private $sendSucc = array();
    public function __construct() {
        require_once(THIRD_CLASS_DIR . '/PHPMailer/class.phpmailer.php');
        $this->mail = new PHPMailer();
        $this->mail->IsHTML();
        $this->mail->CharSet= 'UTF-8';
        switch(Config::get('MAIL_TYPE')) {
            case MAIL_TYPE_SMTP:
                $this->initSmtp();
            break;
            case MAIL_TYPE_SENDMAIL:
                $this->initSendmal();
            break;
            case MAIL_TYPE_MAIL:
                
            break;
        }
    }

    public static function checkMail($mail) {
        if ('.' == substr($mail, -1)) return false;
        $hostName = substr(strchr($mail, '@'), 1);
        $userName = substr($mail, 0, strpos($mail, '@'));
        return preg_match('/^[a-z0-9_\-\.]+$/i', $userName) && checkdnsrr($hostName, 'MX') && preg;
    }


    private function initSmtp() {
        $this->setSmtp();
    }

    private function initSendmail() {
        $this->mail->IsSendmail();
    }

    private function initMail() {
        $this->mail->IsMail();
    }
    
    public function setHostName($hostName) {
        $this->mail->Hostname = $hostName;
    }

    public function setSmtp($host='', $user='', $pass='', $secure='') {
        $this->mail->IsSMTP();
        $this->mail->SMTPAuth = true;
        $this->mail->Host       = $host   ? $host   : Config::get('SMTP_HOST');
        $this->mail->Username   = $user   ? $user   : Config::get('SMTP_USER');
        $this->mail->Password   = $pass   ? $pass   : Config::get('SMTP_PASS');
        $this->mail->SMTPSecure = $secure ? $secure : Config::get('SMTP_SECURE');
    }

    public function setFrom($address, $name='') {
        if (is_array($address)) {
            $this->mail->SetFrom($address[0], $address[1]);
        } else {
            $this->mail->SetFrom($address, $name);
        }
    }

    public function addReceiver($address) {
        if (is_array($address)) {
            $this->mailTo = $address;
        } else {
            $this->mailTo[] = $address;
        }
    }

    public function setSubject($subject) {
        $this->mail->Subject = $subject;
    }

    public function setReply($address, $name='') {
        if (is_array($address)) {
            $this->mail->AddReplyTo($address[0], $address[1]);   
        } else {
            $this->mail->AddReplyTo($address, $name);
        }
    }

    public function setBody($html) {
        $this->mail->MsgHTML($html);
    }

    public function send($to='', $from='', $reply='', $subject='', $contents='') {
        if ('' != $to) {
            $this->mailTo = array();
            $this->addReceiver($to);
        }
        if ('' != $from)        $this->setFrom($from);
        if ('' != $reply)       $this->setReply($reply);
        if ('' != $subject)     $this->setSubject($subject);
        if ('' != $contents)    $this->setBody($contents);

        if (1 == sizeof($this->mailTo)) {
            $this->mail->AddAddress($this->mailTo[0]);
            $sendResult = $this->mail->Send();
            $this->mail->ClearAllRecipients();
            return $sendResult;
        } else {
            $sendSucc = 1;
            $mails = array_chunk($this->mailTo, 50);
            foreach ($mails as $mailTo) {
                $this->mail->ClearBCCs();
                foreach($mailTo as $mail) {
                    $this->mail->AddBCC($mail);
                }
                $sendSucc &= $this->mail->send();
            }
            $this->mail->ClearAllRecipients();
            return $sendSucc;
        }
    }

    public function getSendResult() {
        return array('sendCount'=>$this->sendCount,
                     'sendSucc' =>$this->sendSucc,
                     'sendFail' =>$this->sendFail);
    }
};
?>